import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_app/utils/http.dart';
import 'package:flutter_app/views/homePage.dart';
import 'package:flutter_app/views/login.dart';
import 'package:flutter_app/widget/page_route_anim.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';

import './store.dart';


void main() async{
  // 初始化手机内部存储
  runApp(MyApp());
}

// 路由暂时没封装
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 状态管理
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: new User()),
      ],
      // 路由
      child:OKToast(
        child:MaterialApp(
            title: 'Welcome to Flutter',
            home: MyHomePage(),
            routes: <String, WidgetBuilder>{
              '/page2': (BuildContext context) => new LoginPage(),
              '/homePage':(BuildContext context) => new HomePage(),
              // '/datail':(BuildContext context) => new CardDetail()
            },
            onGenerateRoute:(RouteSettings settings){
              // 路由的钩子，暂未使用
              // ignore: missing_return
              return MaterialPageRoute(builder: (context){
                String routeName = settings.name;
                print(routeName);
                // 如果访问的路由页需要登录，但当前未登录，则直接返回登录页路由，
                // 引导用户登录；其它情况则正常打开路由。
              }
              );
            }
        )
      )
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePage createState() => _MyHomePage();
}

class _MyHomePage extends State<MyHomePage> {

  goto(){
    initLogin();
  }

  initLogin() async{
    ResultData resultData;
    try {
      resultData = await HttpRequest.get('/webauth/jpush/isLogin', null, header:{'Content-Type':'application/json'});
    } on Exception catch (e) {
      print(resultData);
      Navigator.of(context).push(FadeRouteBuilder(LoginPage()));
      return;
    }
    print(resultData);
    if(resultData.code == 0){
      Navigator.of(context).push(FadeRouteBuilder(HomePage()));
    } else{
      Navigator.of(context).push(FadeRouteBuilder(LoginPage()));
    }
  }

  @override
  void initState(){
    super.initState();
    const timeout = const Duration(seconds: 4);
    Timer(timeout, () {
      //到时回调
      goto();
    });
  }

  @override
  Widget build(BuildContext context) {
    // Material is a conceptual piece of paper on which the UI appears.
    return Scaffold(
      body: SafeArea(
        child:Stack(
          //未定位widget占满Stack整个空间
          fit: StackFit.expand,
          alignment:Alignment.center,
          children:[
            Positioned(
              top: 70.0,
              child: Text("智慧园区管理系统", style: TextStyle(fontSize: 24.0,color: Colors.blueAccent, fontWeight: FontWeight.w700))
            ),
            Positioned(
              top: 120.0,
              child: Text("欢迎您", style: TextStyle(fontSize: 36.0,color: Colors.blueAccent, fontWeight: FontWeight.w700,))
            ),
            Image(image: AssetImage('assets/images/welcome.png')),
            Positioned(
              bottom: 40.0,
              child: new RaisedButton(
                color: Colors.blueAccent,
                child: Text("立即体验",style: TextStyle(fontSize: 16.0,color: Colors.white)),
                // 设置按钮圆角
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
                onPressed: goto
              )
            ),
          ]
        )
      )
    );
  }
}



// 封装的头组件，暂时没用了
// class MyAppBar extends StatelessWidget {
//   MyAppBar({this.title});
//
//   // Fields in a Widget subclass are always marked "final".
//
//   final Widget title;
//
//   @override
//   Widget build(BuildContext context) {
//     return Container(
//       height: 156.0, // in logical pixels
//       padding: const EdgeInsets.symmetric(horizontal: 8.0),
//       decoration: BoxDecoration(color: Colors.blue[500]),
//       // Row is a horizontal, linear layout.
//       child: Row(
//         // <Widget> is the type of items in the list.
//         children: <Widget>[
//           IconButton(
//             icon: Icon(Icons.menu),
//             tooltip: 'Navigation menu',
//             onPressed: null, // null disables the button
//           ),
//           // Expanded expands its child to fill the available space.
//           Expanded(
//             child: title,
//           ),
//           IconButton(
//             icon: Icon(Icons.search),
//             tooltip: 'Search',
//             onPressed: null,
//           ),
//         ],
//       ),
//     );
//   }
// }

